/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller.app;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.emoji.EmojiUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.api.config.DistanceUtils;
import com.yami.shop.bean.app.dto.UserDto;
import com.yami.shop.bean.app.dto.UserLikeRecordDto;
import com.yami.shop.bean.app.param.UserInfoParam;
import com.yami.shop.bean.model.UnlockRecord;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserLikeRecord;
import com.yami.shop.common.util.CacheManagerUtil;
import com.yami.shop.dao.UnlockRecordMapper;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.security.enums.App;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.PostRecordService;
import com.yami.shop.service.UserLikeRecordService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/p/user/like")
@Api(tags = "消息接口")
@AllArgsConstructor
public class UserLikeController {

    private final UserService userService;
    private final UserMapper userMapper;
    private final UserLikeRecordService userLikeRecordService;
    @Autowired
    private UnlockRecordMapper unlockRecordMapper;

    @PutMapping("/{id}")
    @ApiOperation(value = "喜欢某用户", notes = "喜欢某用户")
    public ResponseEntity<Void> like(@PathVariable(value = "id") String likeUserId){
        String userId = SecurityUtils.getUser().getUserId();
        //喜欢过不能再喜欢了
        int count = userLikeRecordService.count(Wrappers.lambdaQuery(new UserLikeRecord())
                .eq(UserLikeRecord::getFromUserId, userId)
                .eq(UserLikeRecord::getLikeUserId, likeUserId));
        if(count > 0){
            return ResponseEntity.ok().build();
        }

        UserLikeRecord u = new UserLikeRecord();
        u.setFromUserId(userId);
        u.setLikeUserId(likeUserId);
        u.setCreateTime(new Date());
        userLikeRecordService.save(u);
        userMapper.addOneUserLike(likeUserId);
        return ResponseEntity.ok().build();
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "取消喜欢某用户", notes = "取消喜欢某用户")
    public ResponseEntity<Void> delLike(@PathVariable(value = "id") String likeUserId){
        String userId = SecurityUtils.getUser().getUserId();
        UserLikeRecord one = userLikeRecordService.getOne(Wrappers.lambdaQuery(new UserLikeRecord())
                .eq(UserLikeRecord::getFromUserId, userId)
                .eq(UserLikeRecord::getLikeUserId, likeUserId));
        if(one != null ){
            userLikeRecordService.removeById(one);
            userMapper.subOneUserLike(likeUserId);
        }
        return ResponseEntity.ok().build();
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页获取喜欢的用户", notes = "分页获取喜欢的用户")
    public ResponseEntity< List<UserLikeRecordDto>> page(){
        String userId = SecurityUtils.getUser().getUserId();
        User user = userService.getById(userId);
        List<UserLikeRecordDto> res = userLikeRecordService.getLikeListByUserId(userId);
        for (UserLikeRecordDto re : res) {
            User one = userService.getById(re.getLikeUserId());
            re.setLikeUserName(one.getNickName());
            re.setLikeUserPic(one.getPic());
            if(user.getLng() != null && user.getLat() != null && one.getLng() != null && one.getLat() != null){
                Double dist = DistanceUtils.getDistance(user.getLng(),user.getLat(),one.getLng(),one.getLat());
                re.setDistance(dist);
            }


            Integer sta = unlockRecordMapper.unlUser(userId,one.getUserId());
            if(!sta.equals(0)){
                re.setUnlStatus(1);
            }else{
                re.setUnlStatus(2);
            }

        }

        return ResponseEntity.ok(res);
    }



}
